int Zone1Countadd = 20;
int Zone2Countadd = 30;
int Zone3Countadd = 40;
int Zone4Countadd = 50;

void INITAlarm(){
  datain2.syncPachube();

  Zones[0].Name = "Landing";  
  Zones[0].Pin = A8;
  Zones[0].Eepromadd = Zone1Countadd;
  Zones[0].Count = convint(datain2.getValueByIndex(7));
  Zones[0].Lasttrig = 0;
  Zones[0].Room[0] = 5;
  Zones[0].Trigsent = false;
  Zones[1].Name = "DownHall";  
  Zones[1].Pin = A9;
  Zones[1].Eepromadd = Zone2Countadd;
  Zones[1].Count = convint(datain2.getValueByIndex(8));
  Zones[1].Lasttrig = 0;
  Zones[1].Room[0] = 4;  
  Zones[1].Trigsent = false;
  Zones[2].Name = "LivingRoom";  
  Zones[2].Pin = A10;
  Zones[2].Eepromadd = Zone3Countadd;
  Zones[2].Count = convint(datain2.getValueByIndex(9));
  Zones[2].Lasttrig = 0;
  Zones[2].Room[0] = 1;
  Zones[2].Room[1] = 2;
  Zones[2].Room[2] = 3;  
  Zones[2].Trigsent = false;
  Zones[3].Name = "Kitchen";  
  Zones[3].Pin = A11;
  Zones[3].Eepromadd = Zone4Countadd;
  Zones[3].Count = convint(datain2.getValueByIndex(10));
  Zones[3].Lasttrig = 0;
  Zones[3].Room[0] = 6;    
  Zones[3].Trigsent = false;
  for(int i=0; i<4;i++){
    pinMode(Zones[i].Pin,INPUT);  
    Zones[i].Active = 1;
  }

}

void CheckZones(){
  for(int i=0; i<4;i++){
    int val = analogRead(Zones[i].Pin); 
    Serial.print("Zone ");
    Serial.print(i);
    Serial.print(": ");
    Serial.println(val);
    int ZState=0;
    if(val > 800){
      ZState = 0; 
      if(millis() -  Zones[i].Lasttrig > 300000 && Zones[i].Trigsent == false){
        if(Zones[i].Room[0] == 6){
          sendRF(myCode,Zones[i].Room[0],false,false); 
          sendRF(2,Zones[i].Room[0],false,false);              
          delay(30);  
        }
//        if(Zones[i].Room[0] == 1){
//          for(int j = 0; j<3;j++){
//            sendRF(myCode,Zones[i].Room[j],false,false); 
//            delay(30);
//          }
//        }
//        else{
          sendRF(myCode,Zones[i].Room[0],false,false); 
          delay(30);
//        }
        Zones[i].Trigsent = true;
      }
    }
    else if (val < 800){
      ZState = 1; 
      Zones[i].Lasttrig = millis();
      Zones[i].Trigsent = false;
    }
    if (val == 0){
      ZState = 0;       
    }
    Zones[i].State = ZState; 
    if(Zones[i].Prev != Zones[i].State){
      if (Zones[i].State == 1){
        Zones[i].Count++ ;
        //        EEPROM.write(Zones[i].Eepromadd,Zones[i].Count);
        CheckAlarm();
      }
      Zones[i].Prev = Zones[i].State;
    }

  }
}

void CheckAlarm(){
  if(AlarmState){
    String msg = "The Alarm has been triggered! Zone information is below.";
    msg += "\\n";
    for(int i=0;i<4;i++){
      msg += Zones[i].Name;
      msg += " ";
      msg += Zones[i].State;
      msg += "\\n";
    }
    if(firstmail){
      if(time.current > (time.emailresend + 1200000)) { // 20mins mins

        time.emailresend = time.current;
      }
    }
    else{

      time.emailresend = time.current; 
      firstmail = true;
    }
  }

}











